package com.dz.lm.web.controller.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dz.lm.common.exception.ServiceException;
import com.dz.lm.common.utils.json.JsonUtils;
import com.dz.lm.service.goldagent.GoldAgentService;
import com.dz.lm.web.controller.BasicControl;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping(value = "/h5/weijie/rewriteUrl/goldAgent/", produces = "application/json")
public class GoldAgentH5Controller extends BasicControl {
    public Logger logger = LogManager.getLogger(GoldAgentController.class);

    @Autowired
    private GoldAgentService goldAgentService;
    /**
     * 查看（生成）个人专属店铺链接
     *
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/qryPerStorInfo")
    public Map<String, Object> qryPerStorInfo(HttpServletRequest request) throws ServiceException {
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        JSONObject _reqData = JSONObject.parseObject(reqData);
        JSONObject subjo = new JSONObject();

        Map<String, Object> param = JsonUtils.readValue(_reqData.toString());
        //用户编码
        if (param.get("userId") == null || "".equals(param.get("userId"))) {
            param.put("userId", request.getAttribute("userId"));
        }
        Map<String, Object> perStorInfo = goldAgentService.qryPerStorInfo(param);
        if (perStorInfo != null && !perStorInfo.isEmpty()) {
            subjo.put("result", "00");
            subjo.put("message", "查询成功");
            subjo.put("perOpenStoreFlag", perStorInfo.get("perOpenStoreFlag")== null ? "" : perStorInfo.get("perOpenStoreFlag"));
            subjo.put("perStoreUrl", perStorInfo.get("perStoreUrl")== null ? "" : perStorInfo.get("perStoreUrl"));
            subjo.put("scanCode", perStorInfo.get("scanCode")== null ? "" : perStorInfo.get("scanCode"));
        } else {
            subjo.put("result", "01");
            subjo.put("message", "未查的个人专属店铺链接");
            subjo.put("perOpenStoreFlag", "0");
            subjo.put("perStoreUrl", null);
            subjo.put("scanCode", null);
        }
        return parameterEncrypt(request, subjo);
    }

}
